package br.com.dev.tcc.view.utilitarios;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.SpinnerIpvaItens;
import br.com.dev.tcc.bean.TabelaIpva;
import br.com.dev.tcc.interfaces.UtilitariosItemView;

public class UtilitariosTabelaIpvaView implements UtilitariosItemView {

	private View view;
	private Activity activity;
	private View containerTabela;

	public UtilitariosTabelaIpvaView(Activity activity) {

		this.activity = activity;
		LayoutInflater inflater = LayoutInflater.from(activity);
		view = inflater.inflate(R.layout.utilitarios_ipva_tabela, null);
		
		containerTabela =  view.findViewById(R.id.tabela);
		
		Spinner spinner = (Spinner) view.findViewById(R.id.spinnerIpva);
		
		spinner.setAdapter(new ArrayAdapter<SpinnerIpvaItens>(activity,
				R.layout.spinner_item, SpinnerIpvaItens.values()));
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> a, View v,
					int position, long id) {

				if(position == 0)
					containerTabela.setVisibility(View.GONE);
				else{
					exibeTabela(SpinnerIpvaItens.values()[position].getTabela());
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> a) {
			}
		});
	}
	
	private void exibeTabela(TabelaIpva tabela){
		containerTabela.setVisibility(View.VISIBLE);
		setPrimerasParcelas(tabela);
		setSegundasParcelas(tabela);
		setTerceirasParcelas(tabela);
	}

	private void setPrimerasParcelas(TabelaIpva ipva) {

		String[] parcelas = ipva.getPrimeirasParcelas();
		((TextView) activity.findViewById(R.id.t1_0)).setText(parcelas[9]);
		((TextView) activity.findViewById(R.id.t1_1)).setText(parcelas[0]);
		((TextView) activity.findViewById(R.id.t1_2)).setText(parcelas[1]);
		((TextView) activity.findViewById(R.id.t1_3)).setText(parcelas[2]);
		((TextView) activity.findViewById(R.id.t1_4)).setText(parcelas[3]);
		((TextView) activity.findViewById(R.id.t1_5)).setText(parcelas[4]);
		((TextView) activity.findViewById(R.id.t1_6)).setText(parcelas[5]);
		((TextView) activity.findViewById(R.id.t1_7)).setText(parcelas[6]);
		((TextView) activity.findViewById(R.id.t1_8)).setText(parcelas[7]);
		((TextView) activity.findViewById(R.id.t1_9)).setText(parcelas[8]);
	}

	private void setSegundasParcelas(TabelaIpva ipva) {

		String[] parcelas = ipva.getSegundasParcelas();
		((TextView) activity.findViewById(R.id.t2_0)).setText(parcelas[9]);
		((TextView) activity.findViewById(R.id.t2_1)).setText(parcelas[0]);
		((TextView) activity.findViewById(R.id.t2_2)).setText(parcelas[1]);
		((TextView) activity.findViewById(R.id.t2_3)).setText(parcelas[2]);
		((TextView) activity.findViewById(R.id.t2_4)).setText(parcelas[3]);
		((TextView) activity.findViewById(R.id.t2_5)).setText(parcelas[4]);
		((TextView) activity.findViewById(R.id.t2_6)).setText(parcelas[5]);
		((TextView) activity.findViewById(R.id.t2_7)).setText(parcelas[6]);
		((TextView) activity.findViewById(R.id.t2_8)).setText(parcelas[7]);
		((TextView) activity.findViewById(R.id.t2_9)).setText(parcelas[8]);
	}

	private void setTerceirasParcelas(TabelaIpva ipva) {

		String[] parcelas = ipva.getTerceirasParcelas();
		((TextView) activity.findViewById(R.id.t3_0)).setText(parcelas[9]);
		((TextView) activity.findViewById(R.id.t3_1)).setText(parcelas[0]);
		((TextView) activity.findViewById(R.id.t3_2)).setText(parcelas[1]);
		((TextView) activity.findViewById(R.id.t3_3)).setText(parcelas[2]);
		((TextView) activity.findViewById(R.id.t3_4)).setText(parcelas[3]);
		((TextView) activity.findViewById(R.id.t3_5)).setText(parcelas[4]);
		((TextView) activity.findViewById(R.id.t3_6)).setText(parcelas[5]);
		((TextView) activity.findViewById(R.id.t3_7)).setText(parcelas[6]);
		((TextView) activity.findViewById(R.id.t3_8)).setText(parcelas[7]);
		((TextView) activity.findViewById(R.id.t3_9)).setText(parcelas[8]);
	}

	@Override
	public View getView() {

		return view;
	}
}
